package ins;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class cool {

	
		@SuppressWarnings({ "resource", "unused" })
		public static void main(String[] args) {
			/*
			 * fileInputs = new FileInputStream(file); InputStreamReader
			 * inputStreamReader = new InputStreamReader( fileInputs); fileInputs =
			 * new FileInputStream(file); InputStreamReader inputStreamReader = new
			 * InputStreamReader( fileInputs);
			 */

			// D:\log\log.log
			String path = "C:\\database\\log.log";
			File file = new File(path);
			if (file.exists() && file.isFile()) {
				// main

				FileInputStream fileInputs = null;
				try {
					fileInputs = new FileInputStream(file);
					InputStreamReader inputStreamReader = new InputStreamReader(
							fileInputs);
					BufferedReader bufferedReader = new BufferedReader(
							inputStreamReader);
					String line = null;
					
					File ipFile = new File("C:\\database\\log\\IPs.txt"); 
					//D:\log\ips.txt
					if(!ipFile.exists()){
						ipFile.createNewFile();
					}
					
					FileOutputStream fileOutputStream = new FileOutputStream(ipFile);
					BufferedOutputStream  bufferedOutputStream  = new BufferedOutputStream  (fileOutputStream);
					while ((line = bufferedReader.readLine()) != null) {
						//System.out.println(MainApp.getIP(line));
						
						 
						//bufferedOutputStream.
						fileOutputStream.write(cool.getIP(line).getBytes());
						fileOutputStream.write("\n".getBytes());
						 
					}
					fileOutputStream.flush();
					fileOutputStream.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			} else {
				System.out.println("not a file or not exits!!");
			}
			
			System.out.println("end");
		}

		/**
		 * 从一串文字中提取一个IP
		 * @param content 待处理的一串文字
		 * @return 
		 */
		public static String getIP(String content){
		    String regUrl = "((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))";
	        Pattern p = Pattern.compile(regUrl, Pattern.CASE_INSENSITIVE);
	        Matcher m = p.matcher(content);
	        boolean blnp = m.find();
	        if(blnp==true){// get it
	        	return m.group();
	        }
			return null;
		
		
	



	/*
	 * Returns: A String containing the contents of the line, not including any
	 * line-termination characters, or null if the end of the stream has been
	 * reached
	 */
	}

}